package com.yanze.schoolinformation.view.login_register.login;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.AppConfig;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.databinding.FragmentLoginBinding;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.utils.DialogUtils;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.SPUtils;
import com.yanze.schoolinformation.utils.StringUtils;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.viewModule.login.LoginNavigator;
import com.yanze.schoolinformation.viewModule.login.LoginViewModel;

/**
 * Created by MrZheng on 2018/5/7.
 */

public class LoginFragment extends BaseFragment<FragmentLoginBinding> implements LoginNavigator {

    private LoginViewModel viewModel;

    @Override
    public int setContent() {
        return R.layout.fragment_login;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showContentView();
        viewModel = new LoginViewModel(this);
        viewModel.setmNavigator(this);
        MyOnClick onclick = new MyOnClick();
        bindingView.ivBack.setOnClickListener(onclick);
        bindingView.btnLogin.setOnClickListener(onclick);
        bindingView.tvRegister.setOnClickListener(onclick);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String phone = (String) SPUtils.get(getContext(), C.key.SP_Phone, "");
        if (phone != null && !phone.equals("")) {
            bindingView.cetPhone.setText(phone);
        }
    }

    private class MyOnClick extends PerfectClickListener {

        @Override
        protected void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.iv_back:
                    getActivity().onBackPressed();
                    break;
                case R.id.btn_login:
                    String phone = bindingView.cetPhone.getText().toString();
                    String password = bindingView.petPwd.getText().toString();
                    if (!check(phone, password)) {
                        return;
                    }
                    DialogUtils.showDialog(getContext(), "请稍候...");
                    viewModel.Login(phone, password);
                    break;
                case R.id.tv_register:
                    selectFragment(new RegisterFragment());
                    break;
            }
        }
    }

    public void selectFragment(Fragment fragment) {
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_contant, fragment);
        transaction.addToBackStack("login");
        transaction.commit();
    }


    public boolean check(String phone, String password) {
        if (StringUtils.isEmpty(phone)) {
            bindingView.inputPhone.setError(getString(R.string.please_input_phoneNumber));
            return false;
        }
        bindingView.inputPhone.setError("");
        bindingView.inputPhone.setErrorEnabled(false);
        if (StringUtils.isEmpty(password)) {
            bindingView.inputPwd.setError(getString(R.string.please_input_pwd));
            return false;
        }
        bindingView.inputPwd.setError("");
        bindingView.inputPwd.setErrorEnabled(false);
        return true;
    }

    @Override
    public void registerSuccess(RespUser user) {
        ToastUtils.show(getContext(), "登录成功");
        DialogUtils.dismissDialog();
        getActivity().finish();
        AppConfig.saveUserInfo(getContext(), user.getData());
        SPUtils.put(getContext(), "userid", user.getData().getUserid());
    }

    @Override
    public void registerFaild(String msg) {
        ToastUtils.show(getContext(), msg);
        DialogUtils.dismissDialog();
    }
}
